<?php

namespace App\Http\Controllers\Common;

use App\Http\Controllers\Controller;
use App\Models\Api\v2\GoodsExchangeLog;
use App\Models\Api\v1\Setting;
use Illuminate\Http\Request;

class ApiController extends AppController
{
    protected $where = [];
    protected $limit = 10;
    protected $with = [];
    protected $model = false;
    //微信配置信息
    protected $config = [
        'app_id' => 'wx5574684a9ed00fac',
        'secret' => '23244b9b954b1d59a2c6c830b4d3bca6',

        // 下面为可选项
        // 指定 API 调用返回结果的类型：array(default)/collection/object/raw/自定义类名
//        'response_type' => 'array',
//
//        'log' => [
//            'level' => 'debug',
//            'file' => __DIR__.'/wechat.log',
//        ],
    ];
    public function __construct(Request $request)
    {
        $this->route = [];
        $this->route['modular'] = $request->route('modular');
        $this->route['controller'] = $request->route('controller');
        $this->route['method'] = $request->route('method');
        if ($this->model){
            $model = '\App\model\\'.$this->route['modular'].'\\'.parse_name($this->route['controller']);
            $this->model = new $model();
        }
        if(strstr($this->route['method'],'.')){
            $this->route['method'] = explode('.',$this->route['method'])[0];
        }
    }

    //列表数据
    public function index(Request $request)
    {
        if($this->model == false) return msgerror('模型还未配置');
        $post = param($request);
        if(isset($post['where']) && !empty($post['where']) && is_array($post['where'])){
            foreach ($post['where'] as $k=>$v){
                if(is_array($v)){
                    if(isset($v['key']) && isset($v['value'])){
                        if(isset($v['i'])){
                            $this->where[] = [$v['key'],$v['i'],$v['value']];
                        }else{
                            $this->where[] = [$v['key'],'=',$v['value']];
                        }
                    }
                }else{
                    $this->where[] = [$k,'=',$v];
                }
            }
        }
        $this->limit = $post['limit'] ?? $this->limit;
        $data = $this->model->where($this->where)->orderBy('id','desc')->with($this->with)->paginate($this->limit)->toArray();
        urladd($data);
        return datasuccess($data);
    }
    //数据添加
    public function add(Request $request)
    {
        if($this->model == false) return msgerror('模型还未配置');
        $post = param($request);
        if(!empty($post)){
            $this->model->create($post);
            return datasuccess();
        }else{
            return msgerror('参数缺失');
        }
    }
    //数据修改
    public function upload(Request $request)
    {
        if($this->model == false) return msgerror('模型还未配置');
        $post = param($request);
        if(validate($post,['id'])) return msgerror('参数缺失');
        $id = $post['id'];
        unset($post['id']);
        $this->model->where('id',$id)->update($post);
        return datasuccess();
    }
    //详情接口
    public function view(Request $request)
    {
        if($this->model == false) return msgerror('模型还未配置');
        $post = param($request);
        if(validate($post,['id'])) return msgerror('参数缺失');
        $list = $this->model->with($this->with)->where('id',$post['id'])->first()->toArray();
        urladd($list);
        return datasuccess($list);
    }

    //删除数据
    public function delete(Request $request)
    {
        if($this->model == false) return msgerror('模型还未配置');
        $post = param($request);
        if(validate($post,['id'])) return msgerror('参数缺失');
        if (strstr((string)$post['id'], ',')) {
            $id = explode(',', $post['id']);
        } else {
            $id = [$post['id']];
        }
        $this->model->destroy($id);
        return datasuccess([], '删除成功');
    }


    public function setting($key){
        return (new Setting())->where('var',$key)->value('value');
    }

    public function exchangeLog($cid,$name,$content,$order_no){
        $user = auth('api')->user();
        $time = Date('Y-m-d H:i:s', time());
        $exchangeLogData = [
            'cid' => $cid,
            'name' => $user->nickname . $name,
            'user_id' => $user->id,
            'create_time' => $time,
            'update_time' => $time,
            'content' => $content,
            'order_no'=>$order_no,
            'role' => 1,
            'image' => $user->image,
        ];
        (new GoodsExchangeLog())->create($exchangeLogData);
        return true;
    }



}
